<?php
class Beranda extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('auth');
	}
	public function index()
	{
		if($this->auth->is_logged_in()==false)
		{
			$this->login();
		}
		else
		{
			//load usermodel
			$this->load->model('usermodel');
			//mendapatkan list menu
			$data['menu'] = $this->usermodel->get_menu();
				
			//set variabel title
			$this->template->set('title','Selamat Datang | Aplikasi Persuratan');
			//load fiel view 'index.php' yang ada di folder view/admin ke dalam templte php
			$this->template->load('template','index',$data);
			//$this->load->view('index');
		}
	}
	public function login()
	{
		//$this->load->library('auth');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username','Username','trim|required');
		$this->form_validation->set_rules('password','Password','trim|required');
		$this->form_validation->set_error_delimiters('<span style="color:#ff0000">','</span>');
		
		if($this->form_validation->run()==FALSE)
		{
			$this->template->set('title', 'Login | Aplikasi Persuratan');
			$this->template->load('template','login_form');
			
		}
		else
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$success = $this->auth->do_login($username,$password);
			if($success)
			{
				//masuk ke halaman indeks user
				//echo 'login sukses';
				redirect('beranda/index');
				
			}
			else
			{
				$this->template->set('title',' Login | Aplikasi Persuratan');
				$data['login_info'] = "Maaf, username dan password salah !";
				$this->template->load('template','login_form',$data);
			}
		}
	}
	public function logout()
	{
		if($this->auth->is_logged_in()==TRUE)
		{
			//jika sudah login, destroy session
			$this->auth->do_logout();
		}
			//kembalikan ke halaman utama
			redirect ('beranda');
	}
	public function manajemen_menu()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		//mencegah user mengakses menu yang tidak boleh ia buka
		$this->auth->cek_menu(1);
		//tampilkan isi menu manajemen menu 
		$this->template->set('title','Manajemen User | MyWebApplication.com');
		$this->template->load('template','admin/manajemen_menu');
	}
	public function manajemen_user()
	{
		//mencegah user belum login mengakses halaman ini
		$this->auth->restrict();
		//mencegah user mengakses menu yang tidak boleh ia buka
		$this->auth->cek_menu(2);
		
		//get dari usermodel
		$data['all_user'] = $this->usermodel->get_all_user();
		$this->template->set('title','Manajemen User | My Web Application');
		//load view tabel user di folder application/views/admin
		$this->template->load ('template','admin/tabel_user',$data);
	}
	public function insert_user()
	{
		//load model usermodel
		//$this->load->model('usermodel');
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		//mencegah user mengakses menu tertentu yang boleh dibuka (manajemen user menu_id=2)
		$this->auth->cek_menu(2);
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('nama','nama','trim|required');
		$this->form_validation->set_rules('username','username','trim|required');
		$this->form_validation->set_rules('password','password','trim|required');
		$this->form_validation->set_rules('password_conf','password_conf','trim|required|matches[password]');
		$this->form_validation->set_rules('level','level','trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['level'] = $this->usermodel->get_all_level();
			$this->template->set('title','Tambah User Baru | My Web Application');
			$this->template->load('template','admin/form_input_user',$data);
		}
		else
		{
			$data_user = array (
			'user_nama' =>$this->input->post('nama'),
			'user_username' =>$this->input->post('username'),
			'user_password' =>md5($this->input->post('password')),
			'user_level' =>$this->input->post('level')
			);
			$this->usermodel->insert_data_user($data_user);
			//kembali ke halaman manajemen user
			redirect ('beranda/manajemen_user');
		}
	}
	public function edit_user()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		//mencegah user mengakses menu tertentu yang boleh dibuka (manajemen user menu_id=2)
		$this->auth->cek_menu(2);
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('nama','nama','trim|required');
		$this->form_validation->set_rules('username','username','trim|required');
		$this->form_validation->set_rules('password','password','trim|required');
		$this->form_validation->set_rules('password_conf','password_conf','trim|required|matches[password]');
		$this->form_validation->set_rules('level','level','trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		$id = $this->uri->segment(3);
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['level'] = $this->usermodel->get_all_level();
			//dapatkan user yang akan diedit
			$data['user'] = $this->usermodel->get_user_by_id($id);
			$this->template->set('title','Edit User | My Web Application');
			$this->template->load('template','admin/form_edit_user',$data);
		}
		else
		{
			$data_user = array (
			'user_nama' =>$this->input->post('nama'),
			'user_username' =>$this->input->post('username'),
			'user_password' =>md5($this->input->post('password')),
			'user_level' =>$this->input->post('level')
			);
			$this->usermodel->update_data_user($data_user,$id);
			//kembali ke halaman manajemen user
			redirect ('beranda/manajemen_user');
		}
	}
	public function delete_user()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		//mencegah user mengakses menu tertentu yang boleh dibuka (manajemen user menu_id=2)
		$this->auth->cek_menu(2);
		
		//dapatkan id user dari segment ke-3 dari URI
		$id = $this->uri->segment(3);
		$this->usermodel->delete_user($id);
		//kembali ke halaman manajemen user
		redirect ('beranda/manajemen_user');
	}
}